package com.zw.separate.config;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * @Description 动态数据源配置
 * @Author zw
 * @Date 2021/5/19 16:17
 * @Param
 * @Return
**/
public class DynamicDataSource extends AbstractRoutingDataSource {

	private static final ThreadLocal<DatabaseType> contextHolder = new ThreadLocal<DatabaseType>();

	@Override
	protected Object determineCurrentLookupKey() {
		return contextHolder.get();
	}

	public static enum DatabaseType {
		Master, Slave
	}

	public static void master(){
		contextHolder.set(DatabaseType.Master);
	}


	public static void slave(){
		contextHolder.set(DatabaseType.Slave);
	}

	public static void setDatabaseType(DatabaseType type) {
		contextHolder.set(type);
	}

	public static DatabaseType getType(){
		return contextHolder.get();
	}
}
